package com.example.onlinemonitoring.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.onlinemonitoring.Util.tem.SensorAbstractBaseService;
import com.example.onlinemonitoring.dao.*;
import com.example.onlinemonitoring.model.*;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.alarmmessage;
import com.example.onlinemonitoring.model.common.sensor;
import com.example.onlinemonitoring.query.QueryObject;
import com.example.onlinemonitoring.serviceImpl.*;
import com.github.pagehelper.PageInfo;
import com.example.onlinemonitoring.Util.ConditionDispose;
import com.example.onlinemonitoring.Util.LanguageManager;
import ch.qos.logback.core.joran.util.beans.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(value = "继电器配置接口", tags = { "继电器配置接口" })
@RestController
@RequestMapping("/Jdqconfig")
public class jdqconfigController {
    @Autowired
    AppdeviceServiceImpl appdeviceImpl;
    @Autowired
    JdqconfigServiceImpl jdqconfigserviceimpl;
    @Autowired
    AppstationServiceImpl appstationserviceimpl;
    @Autowired
    BeanFactory beanFactory;
    @Autowired
    LanguageManager languageManager;

    @ApiOperation(value = "查询告警信号的设备名称列表", notes = "查询告警信号的设备名称列表")
    @ResponseBody
    @GetMapping("/selectAllAlarmDevice")
    public MyResult<Jdqconfig> selectAllAlarmDevice() {
        MyResult<Jdqconfig> result = new MyResult<Jdqconfig>();
        List<Jdqconfig> list = new ArrayList<>();
        List<Appdevice> devicelist = new ArrayList<>();
        Example example = new Example(Appdevice.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isshow", 1);
        criteria.andNotEqualTo("type", "转发");
        example.orderBy("ordernum").asc();
        {
            Jdqconfig jdqconfig = new Jdqconfig();
            jdqconfig.setBindstationid(1);
            jdqconfig.setBinddevicegroup(1);
            jdqconfig.setSimplename("all");// 上面一排展示的设备简写
            jdqconfig.setBindname(languageManager.getLan().getNameAll());// 上面一排展示的设备名称
            list.add(jdqconfig);
        }
        devicelist = appdeviceImpl.getList(example).getData();
        for (int j = 0; j < devicelist.size(); j++) {
            Appdevice record = devicelist.get(j);
            int stationid = record.getStationid();
            int devicegroup = record.getDevicegroup();
            {
                Jdqconfig jdqconfig = new Jdqconfig();
                jdqconfig.setBindstationid(stationid);
                jdqconfig.setBinddevicegroup(devicegroup);
                jdqconfig.setSimplename(record.getSimple() + "-" + stationid + "-" + devicegroup);// 上面一排展示的设备简写
                jdqconfig.setBindname(record.getName());// 上面一排展示的设备名称
                list.add(jdqconfig);
            }
        }
        if (list.size() != 0) {
            result.setCode(0);
            result.setMsg("success");
            result.setCount(list.size());
            result.setData(list);
        } else {
            result.setCode(1);
            result.setMsg(languageManager.getLan().getDataEmpty());
        }
        return result;
    }

    @ApiOperation(value = "查询继电器", notes = "查询继电器")
    @ResponseBody
    @GetMapping("/selectConfigData")
    public MyResult<Jdqconfig> selectConfigData(
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        Example example = new Example(Jdqconfig.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", stationid);
        criteria.andEqualTo("devicegroup", devicegroup);
        List<Jdqconfig> list = jdqconfigserviceimpl.selectByExample(example);
        MyResult<Jdqconfig> result = new MyResult<Jdqconfig>();
        if (list.size() != 0) {
            result.setCode(0);
            result.setMsg("success");
            result.setCount(list.size());
            result.setData(list);
        } else {
            result.setCode(1);
            result.setMsg(languageManager.getLan().getDataEmpty());
        }
        return result;
    }

    @ApiOperation(value = "删除继电器", notes = "删除继电器")
    @ResponseBody
    @GetMapping("/deleteDevice")
    public MyResult<Integer> deleteDevice(@RequestParam(value = "id", required = true) Integer id) {
        Jdqconfig record = new Jdqconfig();
        MyResult<Integer> m = new MyResult<Integer>();
        record.setId(id);
        if (jdqconfigserviceimpl.deleteByPrimaryKey(record) >= 1) {
            m.setMsg(languageManager.getLan().getModifySuccess());
            m.setCode(0);
        } else {
            m.setMsg(languageManager.getLan().getModifyFailed());
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "修改继电器", notes = "修改继电器")
    @ResponseBody
    @RequestMapping(value = "/updateDevice", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateDevice(@RequestBody String jsonParam,
            @RequestParam(value = "id", required = true) Integer id) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        Jdqconfig record = object.toJavaObject(Jdqconfig.class);
        MyResult<Integer> m = new MyResult<Integer>();
        record.setStationid(record.getStationid());
        record.setDevicegroup(record.getDevicegroup());
        record.setDbroute(record.getDbroute());
        record.setId(id);
        if (jdqconfigserviceimpl.Update(record, true) >= 1) {
            m.setMsg(languageManager.getLan().getModifySuccess());
            m.setCode(0);
        } else {
            m.setMsg(languageManager.getLan().getModifyFailed());
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "新增继电器", notes = "新增继电器")
    @ResponseBody
    @RequestMapping(value = "/addDevice", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> addDevice(@RequestBody String jsonParam,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        Jdqconfig record = object.toJavaObject(Jdqconfig.class);
        MyResult<Integer> m = new MyResult<Integer>();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        record.setBindstationid(record.getBindstationid());
        record.setBinddevicegroup(record.getBinddevicegroup());
        record.setDbroute(record.getDbroute());
        if (jdqconfigserviceimpl.insert(record) >= 1) {
            List<Integer> res = new ArrayList<>();
            res.add(record.getId());
            m.setData(res);
            m.setMsg(languageManager.getLan().getAddSuccess());
            m.setCode(0);
        } else {
            m.setMsg(languageManager.getLan().getAddFailed());
            m.setCode(1);
        }
        return m;
    }
}
